import { Reducer, Effect, Subscription } from 'umi';
import { fetchStudentsData } from '@/services'; // 导入用于获取学生数据的服务或 API 请求方法

//定义模型的类型，包括 StateType（状态类型）和 DetailScoreModelType（模型类型）包括命名空间、状态、异步效果和同步 reducer
export type StateType = {};

export type DetailScoreModelType = {
  namespace: string;
  state: StateType;
  effects: {
    fetchStudents: Effect;
    fetchSecondclass: Effect;
    fetchSource: Effect;
    fetchCetfail: Effect;
    fetchCetdetail: Effect;
    fetchPrecaution: Effect;
  };
  reducers: {
    saveStudents: Reducer<StateType>;
    saveSecondclass: Reducer<StateType>;
    saveSource: Reducer<StateType>;
    saveCetfail: Reducer<StateType>;
    saveCetdetail: Reducer<StateType>;
    savePrecaution: Reducer<StateType>;
  };
};
//编写模型的具体实现
const DetailScoreModel: DetailScoreModelType = {
  namespace: 'detailScoreModel', // 模型的命名空间
  state: {
    students: [], // 学生成绩详细
    secondclass: [], //第二课堂详细
    source: [], //预警分析
    cetfail: [], //四六级失败分析
    cetdetil: [], //四六级详细
    precaution: [], //预警详情
  },
  effects: {
    *fetchStudents(_, { put }) {
      try {
        // 假设假数据是一个数组形式的学生列表
        const students = [
          {
            id: 1,
            term: '第一学年春季',
            classnum: 10,
            credit: 30,
            gpa: 3.8,
            class: [
              { classname: '语文', curcredit: 6, score: 90 },
              { classname: '矩阵论', curcredit: 5, score: 90 },
              { classname: '物理', curcredit: 3, score: 93 },
              { classname: '化学', curcredit: 3, score: 95 },
              { classname: '生物', curcredit: 3, score: 30 },
              { classname: '政治', curcredit: 1, score: 50 },
              { classname: '历史', curcredit: 1, score: 90 },
              { classname: '地理', curcredit: 1, score: 96 },
              { classname: '体育', curcredit: 1, score: 30 },
              { classname: '音乐', curcredit: 6, score: 40 },
            ],
          },
          {
            id: 2,
            term: '第一学年秋季',
            classnum: 6,
            credit: 20,
            gpa: 3.9,
            class: [
              { classname: '数据结构', curcredit: 6, score: 90 },
              { classname: '操作系统', curcredit: 5, score: 90 },
              { classname: '计算机组成', curcredit: 3, score: 93 },
              { classname: '计算机网络', curcredit: 3, score: 95 },
              { classname: '编译原理', curcredit: 3, score: 30 },
              { classname: '网络安全', curcredit: 1, score: 50 },
            ],
          },
          {
            id: 3,
            term: '第二学年春季',
            classnum: 5,
            credit: 19,
            gpa: 3.6,
            class: [
              { classname: '高等数学', curcredit: 6, score: 86 },
              { classname: '矩阵论', curcredit: 5, score: 90 },
              { classname: '概率统计', curcredit: 3, score: 93 },
              { classname: '英文写作与翻译', curcredit: 2, score: 78 },
              { classname: '大学物理', curcredit: 3, score: 30 },
            ],
          },
          {
            id: 4,
            term: '第二学年秋季',
            classnum: 3,
            credit: 16,
            gpa: 3.5,
            class: [
              { classname: '高级机器学习', curcredit: 6, score: 90 },
              { classname: '深度学习', curcredit: 5, score: 90 },
              { classname: '神经网络', curcredit: 3, score: 93 },
            ],
          },
        ];
        yield put({
          type: 'saveStudents',
          payload: students, // payload携带数据
        });
      } catch (error) {
        // 错误处理
      }
    },
    *fetchSecondclass(_, { put }) {
      try {
        // 假设假数据是一个数组形式的学生列表
        const secondclass = [
          {
            id: 1,
            category: '理想信念',
            term: '第一学年春季',
            activename: '军运会志愿者',
            level: 1,
            score: 88,
          },
          {
            id: 2,
            category: '实践服务',
            term: '第一学年秋季',
            activename: '大学生创新创业',
            level: 2,
            score: 90,
          },
          {
            id: 3,
            category: '文化艺术',
            term: '第二学年春季',
            activename: '挑战杯',
            level: 3,
            score: 99,
          },
          {
            id: 4,
            category: '学术科技',
            term: '第二学年秋季',
            activename: '数学奥林匹克',
            level: 2,
            score: 89,
          },
          {
            id: 5,
            category: '社会工作',
            term: '第三学年春季',
            activename: '亚运会志愿者',
            level: 1,
            score: 100,
          },
          {
            id: 6,
            category: '体育素质',
            term: '第三学年秋季',
            activename: '大学生运动会',
            level: 1,
            score: 72,
          },
        ];
        yield put({
          type: 'saveSecondclass',
          payload: secondclass, // payload携带数据
        });
      } catch (error) {
        // 错误处理
      }
    },
    *fetchSource(_, { put }) {
      try {
        // 假设假数据是一个数组形式的学生列表
        const source = [
          { class: '班级1', level1: 43.3, level2: 35.5, level3: 17.5 },
          { class: '班级2', level1: 25.6, level2: 12.4, level3: 3.8 },
          { class: '班级3', level1: 18.2, level2: 22.6, level3: 0.7 },
        ];
        yield put({
          type: 'saveSource',
          payload: source, // payload携带数据
        });
      } catch (error) {
        // 错误处理
      }
    },
    *fetchCetfail(_, { put }) {
      try {
        // 假设假数据是一个数组形式的学生列表
        const cetfail = [
          { class: '班级1', cet4: 0.1, cet6: 0.2 },
          { class: '班级2', cet4: 0.2, cet6: 0.3 },
          { class: '班级3', cet4: 0.05, cet6: 0.25 },
        ];
        yield put({
          type: 'saveCetfail',
          payload: cetfail, // payload携带数据
        });
      } catch (error) {
        // 错误处理
      }
    },
    *fetchCetdetail(_, { put }) {
      try {
        // 假设假数据是一个数组形式的学生列表
        const cetdetail = [
          {
            order: 1,
            name: '晴天',
            studentid: '202232805099',
            class: '电气1801',
            grade: 2022,
            major: '电气工程与自动化',
            tutor: '周杰伦',
            cet4: '655',
            cet4ispass: '通过',
            cet6: '565',
            cet6ispass: '通过',
          },
          {
            order: 2,
            name: '花海',
            studentid: '202232805088',
            class: '智能1801',
            grade: 2022,
            major: '人工智能',
            tutor: '周杰伦',
            cet4: '635',
            cet4ispass: '通过',
            cet6: '585',
            cet6ispass: '通过',
          },
        ];
        yield put({
          type: 'saveCetdetail',
          payload: cetdetail, // payload携带数据
        });
      } catch (error) {
        // 错误处理
      }
    },
    *fetchPrecaution(_, { put }) {
      try {
        // 假设假数据是一个数组形式的学生列表
        const precaution = [
          {
            key: '1',
            warning: '1',
            flunk: '4',
            name: '李四',
            studentid: '202232805099',
            class: '电气1801',
            grade: '2022',
            major: '电气工程与自动化',
            tutor: '周杰伦',
          },
          {
            key: '2',
            warning: '0',
            flunk: '0',
            name: '张三',
            studentid: '201813137022',
            class: '计科1801',
            grade: '2022',
            major: '计算机科学与技术',
            tutor: '周杰伦',
          },
        ];
        const cousefail = [
          {
            key: '1',
            课组名: '课组A',
            课程号: 'C001',
            课程名: '数学',
            课程属性: '必修',
            计划学期: '第一学期',
            学分: 4,
            学时: 72,
            成绩: 50,
          },
          {
            key: '2',
            课组名: '课组B',
            课程号: 'C002',
            课程名: '英语',
            课程属性: '选修',
            计划学期: '第二学期',
            学分: 3,
            学时: 54,
            成绩: 45,
          },
        ];
        const studetail = [
          {
            key: '1',
            姓名: '方文山',
            学号: '202232805099',
            班级: '自动化2022',
            学生电话: '2521643230',
            专业: '电气工程与自动化',
            年级: 2022,
            家长姓名: '叶惠美',
            家长电话: '1360590955',
          },
        ];
        const payload = {
          precaution: precaution,
          cousefail: cousefail,
          studetail: studetail,
        };
        yield put({
          type: 'savePrecaution',
          payload: payload, // payload携带数据
        });
      } catch (error) {
        // 错误处理
      }
    },
  },
  //在仓库中的 detailScoreModel 对象中更新 students 数据，使得组件可以通过连接 Redux 仓库获取最新的 students 数据
  reducers: {
    saveStudents(state, { payload }) {
      return {
        ...state,
        students: payload,
      };
    },
    saveSecondclass(state, { payload }) {
      return {
        ...state,
        secondclass: payload,
      };
    },
    saveSource(state, { payload }) {
      return {
        ...state,
        source: payload,
      };
    },
    saveCetfail(state, { payload }) {
      return {
        ...state,
        cetfail: payload,
      };
    },
    saveCetdetail(state, { payload }) {
      return {
        ...state,
        cetdetail: payload,
      };
    },
    savePrecaution(state, { payload }) {
      return {
        ...state,
        precaution: payload.precaution,
        cousefail: payload.cousefail,
        studetail: payload.studetail,
      };
    },
  },
};

export default DetailScoreModel;
